package com.smart.home.web;

import com.smart.home.properties.DevProperties;
import com.smart.home.properties.ShellProperties;
import com.smart.home.vo.DeviceVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
public class TfCardController {

    private final Logger logger = LoggerFactory.getLogger(TfCardController.class);

    @Resource
    private DevProperties devProperties;

    @Resource
    private ShellProperties shellProperties;


    @PostMapping("/firmware")
    public int firmware(@RequestBody DeviceVo vo) {
        try {
            if (StringUtils.isEmpty(vo.getCode())) {
                logger.error("code不能为空");
                return 3;
            }
            if (StringUtils.isEmpty(vo.getIp())) {
                logger.error("ip不能为空");
                return 3;
            }
            if (StringUtils.isEmpty(vo.getPort())) {
                logger.error("端口不能为空");
                return 3;
            }
            logger.info("params:{}", vo);
            for (Map<String, String> entry : devProperties.getList()) {
                String code = entry.get("code");
                if (Objects.equals(code, vo.getCode())) {
                    vo.setSdaPath(entry.get("sdaPath"));
                    vo.setShellPath(entry.get("shellPath"));
                    vo.setMountPath(entry.get("mountPath"));
                    vo.setBinPath(entry.get("binPath"));
                    vo.setExecPath(entry.get("execPath"));
                    vo.setFirmware(entry.get("firmware"));
                    break;
                }
            }

            List<String> objects = new ArrayList<>();
            objects.add(shellProperties.getCmd());
            objects.add(vo.getMountPath());
            objects.add(vo.getIp());
            objects.add(vo.getPort());
            objects.add(vo.getShellPath());
            objects.add(vo.getBinPath());
            objects.add(vo.getSdaPath());
            objects.add(vo.getExecPath());
            objects.add(vo.getFirmware());
            logger.info("---------------json:{}--------", vo);
            String res = process(shellProperties.getPath(), objects);
            if (res.contains("分区创建失败")) {
                return 1;
            }
            if (res.contains("执行失败")) {
                return 3;
            }
        } catch (Exception e) {
            logger.error("设置失败", e);
            return 3;
        }
        return 2;
    }

    private String process(String path, List<String> command) throws Exception {
        ProcessBuilder pb = new ProcessBuilder();
        pb.directory(new File(path));
        pb.command(command);
        Process process = pb.start();
        process.waitFor();
        String res;
        StringBuilder stringBuilder = new StringBuilder();
        InputStream input = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        while ((res = reader.readLine()) != null) {
            stringBuilder.append(res).append("\n");
        }
        logger.info("result:{}", stringBuilder);
        return stringBuilder.toString();
    }
}
